package org.catdroid.bomba.server.dao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.catdroid.bomba.server.bean.Player;
import org.catdroid.bomba.server.bean.Players;
import org.catdroid.bomba.server.util.DatabaseUtil;

public class PlayerDao {
	public static void insertPlayer(Player player) throws SQLException {
		Connection conn = DatabaseUtil.getSimpleConnection();
		PreparedStatement statement = null;
		statement = conn.prepareStatement("INSERT INTO players (id,name,score,ready,finished,last_request) VALUES (?,?,?,?,?,?)");
		statement.setString(1, player.getId());
		statement.setString(2, player.getName());
		statement.setInt(3, player.getScore());
		statement.setBoolean(4,player.isReady());
		statement.setBoolean(5,player.isFinished());
		statement.setDate (6, player.getRequestDate());
		statement.execute();
		DatabaseUtil.CloseResource(statement);
		DatabaseUtil.CloseResource(conn);

	}
	
	public static Player getPlayer (String id) throws SQLException {
		Player player=null;
		Connection conn = DatabaseUtil.getSimpleConnection();
		PreparedStatement statement = null;
		statement = conn.prepareStatement("SELECT id,name,score,ready,finished from players where id=?");
		statement.setString(1,id);
		ResultSet result=statement.executeQuery();
		if (result.first()) {
			player=rowToPlayer(result);
		}
		DatabaseUtil.CloseResource(statement);
		DatabaseUtil.CloseResource(conn);
		return player;
	}
	
	public static Players getPlayers () throws SQLException {
		Players players=new Players();
		Connection conn = DatabaseUtil.getSimpleConnection();
		PreparedStatement statement = null;
		statement = conn.prepareStatement("SELECT id,name,score,ready,finished from players");
		ResultSet result=statement.executeQuery();
		while (result.next()) {
			players.add(rowToPlayer(result));
		}
		DatabaseUtil.CloseResource(statement);
		DatabaseUtil.CloseResource(conn);
		return players;
	}

	private static Player rowToPlayer(ResultSet result) throws SQLException {
		Player player;
		player=new Player();
		player.setId(result.getString(1));
		player.setName(result.getString(2));
		player.setScore(result.getInt(3));
		player.setReady(result.getBoolean(4));
		player.setFinished(result.getBoolean(5));
		return player;
	}
	
	public static void setPlayerScore(String id, int score) throws SQLException {
		Connection conn = DatabaseUtil.getSimpleConnection();
		PreparedStatement statement = null;
		statement = conn.prepareStatement("UPDATE players set score=? where id=?");
		statement.setInt(1, score);
		statement.setString(2,id);
		statement.execute();
		DatabaseUtil.CloseResource(statement);
		DatabaseUtil.CloseResource(conn);
		
	}
	public static void setPlayerUpdate(Player p) throws SQLException {
		Connection conn = DatabaseUtil.getSimpleConnection();
		PreparedStatement statement = null;
		statement = conn.prepareStatement("UPDATE players set ready=? where id=?");
		statement.setInt(1, p.isReady()?1:0 );
		statement.setString(2,p.getId());
		statement.execute();
		DatabaseUtil.CloseResource(statement);
		DatabaseUtil.CloseResource(conn);
		
	}
	
	public static void resetPlayers() throws SQLException {
		Connection conn = DatabaseUtil.getSimpleConnection();
		PreparedStatement statement = null;
		statement = conn.prepareStatement("Delete * from players");
		statement.execute();
		DatabaseUtil.CloseResource(statement);
		DatabaseUtil.CloseResource(conn);
		
	}
}
